<%@ include file="/WEB-INF/jsp/taglib.jsp" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>钱包</title>

    <!-- Bootstrap -->
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link href="/css/wallet.css" rel="stylesheet">
    <!--[if lt IE 9]>
    <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <script type="text/javascript" src="/js/web3.min.js"></script>
    <script type="text/javascript" src="/js/bignumber.js"></script>

    <script src="/js/jquery-2.2.3.min.js"></script>
    <script src="/js/bootstrap.min.js"></script>

    <script type="text/javascript" src="/js/wallet.js"></script>

    <script type="text/javascript">
        $(function () {

            // 初始化web3
            if (typeof web3 !== 'undefined') {
                web3 = new Web3(web3.currentProvider);
            } else {
                web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
            }

            var EDUContract = web3.eth.contract([ { "constant": true, "inputs": [], "name": "name", "outputs": [ { "name": "", "type": "string", "value": "edu" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_spender", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "approve", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "totalSupply", "outputs": [ { "name": "", "type": "uint256", "value": "1000" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_from", "type": "address" }, { "name": "_to", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "transferFrom", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "decimals", "outputs": [ { "name": "", "type": "uint8", "value": "2" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_value", "type": "uint256" } ], "name": "burn", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [ { "name": "", "type": "address" } ], "name": "balanceOf", "outputs": [ { "name": "", "type": "uint256", "value": "0" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_from", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "burnFrom", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "symbol", "outputs": [ { "name": "", "type": "string", "value": "edu" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_to", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "transfer", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [ { "name": "_spender", "type": "address" }, { "name": "_value", "type": "uint256" }, { "name": "_extraData", "type": "bytes" } ], "name": "approveAndCall", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [ { "name": "", "type": "address" }, { "name": "", "type": "address" } ], "name": "allowance", "outputs": [ { "name": "", "type": "uint256", "value": "0" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "addr", "type": "address" } ], "name": "getBalance", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "name": "initialSupply", "type": "uint256", "index": 0, "typeShort": "uint", "bits": "256", "displayName": "initial Supply", "template": "elements_input_uint", "value": "1000" }, { "name": "tokenName", "type": "string", "index": 1, "typeShort": "string", "bits": "", "displayName": "token Name", "template": "elements_input_string", "value": "edu" }, { "name": "decimalUnits", "type": "uint8", "index": 2, "typeShort": "uint", "bits": "8", "displayName": "decimal Units", "template": "elements_input_uint", "value": "2" }, { "name": "tokenSymbol", "type": "string", "index": 3, "typeShort": "string", "bits": "", "displayName": "token Symbol", "template": "elements_input_string", "value": "edu" } ], "payable": false, "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "from", "type": "address" }, { "indexed": true, "name": "to", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Transfer", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "from", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Burn", "type": "event" } ]);
            var EDU = EDUContract.at("0x4ca024FdA1fF2d4419d51f07D588add5D37b67ab");

            // 获取后台传递过来的地址
            var address = '${address}';

            var etherBalance = web3.fromWei(web3.eth.getBalance(address).toNumber(), 'ether');
            document.getElementById("etherBalance").innerText = etherBalance + " Ether";




            var eduBalance = EDU.getBalance.call(address);
            document.getElementById("eduBalance").innerText = eduBalance + " EDU";


            var sendTransactionList = document.getElementById("send_transaction");
            var receiveTransactionList = document.getElementById("receive_transaction");
            var sendTransactionStr = "";
            var receiveTransactionStr = "";

            var myEvent = EDU.Transfer(null, {fromBlock: 0, toBlock: 'latest',address:address});
            var myResults = myEvent.get(function(error, logs){

                console.log(logs);

                for(var i = 0; i < logs.length; i++) {

                    var transactionHash = "<a href='/wallet/transactions/" + logs[i].transactionHash + "' class='record-main'>" + logs[i].transactionHash + "</a>";

                    if (logs[i].args.to == address) {
                        sendTransactionStr = sendTransactionStr + "<tr class='record-item'>" +
                            "<td><div class='record-content record-hash'>" + transactionHash + "</div></td>" +
                            "<td><div class='record-content record-way'>" + logs[i].args.to + "</div></td>" +
                            "<td><div class='record-content record-number'>" + logs[i].args.value + "&nbsp;&nbsp;&nbsp;&nbsp;(EDU)" + "</div></td>" +
                            // "<td><div class='record-content record-time'>" + "Sssss" + "</div></td>" +
                            "</tr>";
                    }else if (logs[i].args.from == address) {
                        receiveTransactionStr = receiveTransactionStr + "<tr class='record-item'>" +
                            "<td><div class='record-content record-hash'>" + transactionHash + "</div></td>" +
                            "<td><div class='record-content record-way'>" + logs[i].args.from + "</div></td>" +
                            "<td><div class='record-content record-number'>" + logs[i].args.value + "&nbsp;&nbsp;&nbsp;&nbsp;(EDU)" + "</div></td>" +
                            // "<td><div class='record-content record-time'>" + "Sssss" + "</div></td>" +
                            "</tr>";
                    }

                    sendTransactionList.innerHTML = sendTransactionStr;
                    receiveTransactionList.innerHTML = receiveTransactionStr;
                }


            });
            var url = "https://etherchain.org/api/account/" + address + "/tx/0";
            // var url = "https://etherchain.org/api/account/0x0000000000000000000000000000000000000027/tx/0";
            $.getJSON(url, function (json) {

                var transactions = json.data;

                for (var i = 0; i < transactions.length; i++) {
                    var transactionHash = "<a href='/wallet/transactions/" + transactions[i].hash + "' class='record-main'>" + transactions[i].hash + "</a>";

                    if (transactions[i].sender == address) {
                        sendTransactionStr = sendTransactionStr + "<tr class='record-item'>" +
                            "<td><div class='record-content record-hash'>" + transactionHash + "</div></td>" +
                            "<td><div class='record-content record-way'>" + transactions[i].recipient + "</div></td>" +
                            "<td><div class='record-content record-number'>" + web3.fromWei(transactions[i].amount, 'ether') + "&nbsp;&nbsp;&nbsp;&nbsp;(Ether)</div></td>" +
                            // "<td><div class='record-content record-time'>" + transactions[i].time + "</div></td>" +
                            "</tr>";
                    }

                    if (transactions[i].recipient == address) {
                        receiveTransactionStr = receiveTransactionStr + "<tr class='record-item'>" +
                            "<td><div class='record-content record-hash'>" + transactionHash + "</div></td>" +
                            "<td><div class='record-content record-way'>" + transactions[i].sender + "</div></td>" +
                            "<td><div class='record-content record-number'>" + web3.fromWei(transactions[i].amount, 'ether') + "&nbsp;&nbsp;&nbsp;&nbsp;(Ether)</div></td>" +
//                            "<td><div class='record-content record-time'>" + transactions[i].time + "</div></td>" +
                            "</tr>";
                    }
                }

                sendTransactionList.innerHTML = sendTransactionStr;
                receiveTransactionList.innerHTML = receiveTransactionStr;
            });
        });
    </script>

    <c:import url="/WEB-INF/jsp/frontend/head.jsp" />
    <c:import url="/WEB-INF/jsp/frontend/activity/script.jsp" />
</head>
<body>
<div class="head">
    <c:import url="/WEB-INF/jsp/frontend/header.jsp"/>
</div>

<div class="container-wrap wallet-body">
    <div class="container">
        <div class="row account-container">
            <div class="col-md-9 account-selector">
                <span class="account-title wallet-h1">账户地址</span>
                <div class="account">${address}</div>
            </div>
            <div class="col-md-3 wallet-info">
                <img class="img-circle wallet-portrait" src="/images/wallet/wallet-portrait.png">
                <span class="wallet-h1 wallet-username">${username}</span>
            </div>
            <div class="col-md-11 wallet-divider"></div>
            <div class="col-md-9 wallet-remainder">
                <div class="row">
                    <div class="col-md-4 wallet-h2 remainder-title">余额(Ether)：</div>
                    <div class="col-md-8 remainder-number" id="etherBalance"></div>
                </div>
                <div class="row">
                    <div class="col-md-4 wallet-h2 remainder-title">余额(EDU)：</div>
                    <div class="col-md-8 remainder-number" id="eduBalance"></div>
                </div>
            </div>
            <div class="col-md-2">
                <a href="<spring:url value='/wallet/send/${address}'/>" class="wallet-send">发送交易</a>
            </div>
            <div class="col-md-11 wallet-divider"></div>
            <div class="col-md-11 wallet-record">
                <div class="row">
                    <div class="col-md-12 wallet-h1 record-caption">交易记录</div>
                </div>
                <div class="row">
                    <div class="col-md-4 record-panel record-send">发送的交易</div>
                    <div class="col-md-4 record-panel record-receive">接收的交易</div>
                </div>
                <div class="row record-form">

                    <div class="col-md-12 table-responsive send-container">
                        <table class="table send-content">
                            <tr class="record-title">
                                <td class="record-content record-hash">哈希值</td>
                                <td class="record-content record-way">送到</td>
                                <td class="record-content record-number">数额</td>
                                <%--<td class="record-content record-time">时间</td>--%>
                            </tr>
                            <tbody id="send_transaction">

                            </tbody>
                        </table>
                    </div>

                    <div class="col-md-12 table-responsive receive-container">
                        <table class="table receive-content">
                            <thead>
                            <tr class="record-title">
                                <th class="record-content record-hash">哈希值</th>
                                <th class="record-content record-way">来自</th>
                                <th class="record-content record-number">数额</th>
                                <%--<th class="record-content record-time">时间</th>--%>
                            </tr>
                            </thead>

                            <tbody id="receive_transaction">

                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>
